﻿using System.Linq;
using System.Text.RegularExpressions;

namespace TamoCRM.Core
{
    public class Util
    {
        public static bool IsDigitsOnly(string str)
        {
            foreach (var c in str)
            {
                if (c < '0' || c > '9')
                    return false;
            }

            return true;
        }
        public static bool ValidateEmail(string email)
        {
            return ValidateFormat(email, Constant.EmailFormat);
        }
        public static bool ValidateMobile(string mobile)
        {
            if (mobile.Length > 8 && mobile.Length < 11)
            {
                if (IsDigitsOnly(mobile)) return true;
            }
            return false;
        }
        public static string CleanAlphabetAndFirstZero(string input)
        {
            if (string.IsNullOrEmpty(input)) return string.Empty;
            input = input.Replace("O", "0");
            input = input.Replace("o", "0");
            var arr = input.Where(char.IsNumber).ToArray();
            var retVal = new string(arr);
            if (retVal.StartsWith("0")) retVal = retVal.Substring(1);
            if (retVal.StartsWith("84")) retVal = retVal.Substring(2);
            return retVal;
        }
        public static bool ValidateFormat(string input, string format)
        {
            var r = new Regex(format, RegexOptions.Compiled);
            return r.IsMatch(input);
        }        
    }
}
